<?php

namespace app\api\admin\v1\provider;

use extend\docker\Container;

class CloudappProvider
{
    public function getList(string $matchId = "")
    {
        $container = new Container();
        $list = $container->getList();
        $appList = [];

        foreach($list as $v){
            $externalNetwork = [];
            foreach($v["ports"] as $port){
                if(isset($port['publicport']) && $port['publicport']){
                    $externalNetwork[] = ($port['publicport'] ?? "-").'/'.$port['type'] ?? "";
                }
            }
            $id = substr($v['id'], 0, 12);
            $appData = [
                'id' => $id,
                'service_name' => str_replace('/', '', $v['names'][0]),
                'internal_network' => $v['networksettings']['networks']['docker_zhanshop-network']['ipaddress'] ?? "",
                'external_network' => implode(',', $externalNetwork),
                'running_state' => $v['state'],
                'description' => $v['labels']['description'] ?? '',
                'start_time' => $v['status'],
                'create_time' => $v['created'],
            ];
            if($id == $matchId){
                return $appData;
            }
            $appList[] = $appData;
        }

        return [
            'total' => count($appList),
            'list' => $appList
        ];
    }
}